﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using MacViet.DataBaseAPI;

public partial class Control_Employees_Edit_Employee : System.Web.UI.UserControl
{
    //string EMP_ID = Request.QueryString["EMP_ID"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string EMP_ID = Request.QueryString["EMP_ID"].ToString();
            loadEmployee(EMP_ID);
        }
    }

    public void loadEmployee(string EMP_ID)
    {
        EMPLOYEESDataAPI api = new EMPLOYEESDataAPI();
        DataTable db = api.getByEMP_ID(int.Parse(EMP_ID)); 
        if(db != null)
        {
            tbxFirstName.Text = db.Rows[0]["EMP_FIRSTNAME"].ToString();
            tbxLastName.Text = db.Rows[0]["EMP_LASTNAME"].ToString();
            tbxAddress.Text = db.Rows[0]["EMP_ADDRESS"].ToString();
            tbxPhone.Text = db.Rows[0]["EMP_PHONE"].ToString();
            tbxStartDate.Text = db.Rows[0]["EMP_STARTDATE"].ToString();
            tbxLicense.Text = db.Rows[0]["EMP_LICENSE"].ToString();
            tbxPosition.Text = db.Rows[0]["EMP_POSITION"].ToString();
            tbxCommission.Text = db.Rows[0]["EMP_COMMISSION"].ToString();
        }
    }
    protected void ResetBtn_Click(object sender, EventArgs e)
    {
        tbxFirstName.Text = "";
        tbxLastName.Text = "";
        tbxAddress.Text = "";
        tbxPhone.Text = "";       
        tbxLicense.Text = "";
        tbxPosition.Text = "";
    }
    protected void SaveBtn_Click(object sender, EventArgs e)
    {
        try
        {
           

            int EMP_ID = int.Parse(Request.QueryString["EMP_ID"].ToString());
          
            string firstName = tbxFirstName.Text;
            string lastName = tbxLastName.Text;
            string address = tbxAddress.Text;
            string phone = tbxPhone.Text;
            DateTime startDate = Convert.ToDateTime(tbxStartDate.Text);
            string license = tbxLicense.Text;
            string position = tbxPosition.Text;
            int commission = int.Parse(tbxCommission.Text);
            string checkPhone = phone.Trim();

            EMPLOYEESDataAPI api = new EMPLOYEESDataAPI();            
            DataTable db =  api.getACC_IDByPK(EMP_ID);
            int acc_id = Convert.ToInt32(db.Rows[0][0]);

            DataTable dt = new DataTable();
            //dt = api.checkEmployee(acc_id, checkPhone);
            if (dt.Rows.Count >= 1)
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "This employee already exists. Please check your input again!";
            }
            else
            {
                api.updateEMPLOYEES(EMP_ID, acc_id, firstName, lastName, address, phone, startDate, license, position, commission);
                lblMessage.ForeColor = System.Drawing.Color.Blue;
                lblMessage.Text = "Update Successfully!";
            }            
        }
        catch (Exception ex)
        {
            ex.ToString();
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Text = "Please check your input again!";        
        }
    }
}
